package com.secneo.appshare.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.secneo.appshare.R;
import com.secneo.appshare.activities.AppsOtherUserActivity;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.Feed;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;

public class FeedsReceivedAttentAdapter extends BaseAdapter {

	private List<Feed> mFeeds;
	private Context mContext;
	private Handler mHandler;
	private String recentUid;
	private String finishedUid;
	
	
	public FeedsReceivedAttentAdapter(Context ctx, Handler handler, List<Feed> feeds) {
		this.mContext = ctx;
		this.mHandler = handler;
		this.mFeeds = feeds;
		
	}
	
	public void freeDrawable()
	{
		for (int i = 0; i < mFeeds.size(); i++)
		{
			if (!mFeeds.get(i).user_icon.contains("buildin") &&
					mFeeds.get(i).user_icon_drawable != null)
			{
				BitmapDrawable a = (BitmapDrawable) mFeeds.get(i).user_icon_drawable;
				if (a != null && !a.getBitmap().isRecycled())
					a.getBitmap().recycle();
				mFeeds.get(i).user_icon_drawable = null;
			}
		}
		//mFeeds.clear();
		System.gc();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mFeeds.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mFeeds.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public String getFinishedUid() {
		return finishedUid;
	}

	
	public void setFeedList(List<Feed> feeds){
		mFeeds = feeds;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		RequestAttentItem attentItem;
		final Feed feed = mFeeds.get(position);
		if (convertView == null) {
			View v = LayoutInflater.from(mContext).inflate(R.layout.appshare_request_attent_list_item, null);
			
			attentItem = new RequestAttentItem();
			attentItem.mUserHeaderIcon = (ImageView)v.findViewById(R.id.user_icon);
			attentItem.weiboIcon = (ImageView)v.findViewById(R.id.weibo_Icon);
			attentItem.usernameText = (TextView)v.findViewById(R.id.user_name);
			attentItem.attentStatusText = (TextView)v.findViewById(R.id.attent_status_text);
			attentItem.attentButton = (Button)v.findViewById(R.id.btnRequestAttent);
			attentItem.attentIcon = (ImageView)v.findViewById(R.id.attent_icon);
			attentItem.attentCheckBox = (CheckBox)v.findViewById(R.id.AttentCheckBox);
						
			v.setTag(attentItem);
			convertView = v;
		} else {
			attentItem = (RequestAttentItem)convertView.getTag();
		}
		// set icon
		if (feed.user_icon_drawable == null) 
		{
			attentItem.mUserHeaderIcon.setImageResource(R.drawable.some_user);
		} 
		else 
		{
			if(feed.snsType != 0)
			{
				int iconResource = LocalStore.getLocaleStoreInstance().getSmallIconResource(feed.snsType);
				if(iconResource!=0){
					attentItem.weiboIcon.setImageResource(iconResource);
					attentItem.weiboIcon.setVisibility(View.VISIBLE);
				}
			}
			else
			{
				attentItem.weiboIcon.setVisibility(View.INVISIBLE);
			}
			
			attentItem.mUserHeaderIcon.setImageDrawable(feed.user_icon_drawable);
		}
		attentItem.mUserHeaderIcon.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				Log.e("MyTag", "mUserHeaderIcon:"+feed.user_name);
				/*
				RemoteApiImpl remoteApi = new RemoteApiImpl();
				String userId = LocalStore.getLocaleStoreInstance().getUid();
				String emailString = remoteApi.getEmail(mContext, userId);
				*/
				Intent intent = new Intent();
				intent.setClass(mContext, AppsOtherUserActivity.class);

				Bundle mBundle = new Bundle();
				String uid = LocalStore.getLocaleStoreInstance(mContext).getUid();
				mBundle.putString("uid", uid);
				mBundle.putString("other_uid", feed.uid);
				mBundle.putString("user_icon", feed.user_icon);
				mBundle.putString("user_name", feed.user_name);
				intent.putExtras(mBundle);
				mContext.startActivity(intent);
			}
		});
		
		// set name
		attentItem.usernameText.setText(feed.user_name);
		if(1==feed.is_attent){
			attentItem.attentStatusText.setText("已关注对方");
			attentItem.attentIcon.setVisibility(View.VISIBLE);
			attentItem.attentButton.setVisibility(View.GONE);
			attentItem.attentCheckBox.setVisibility(View.INVISIBLE);
		}
		else{
			attentItem.attentStatusText.setText("");
			attentItem.attentIcon.setVisibility(View.GONE);
			attentItem.attentButton.setVisibility(View.VISIBLE);
			attentItem.attentCheckBox.setVisibility(View.VISIBLE);
		}
		
		attentItem.attentButton.setText("+关注");
		attentItem.attentButton.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				Log.e("MyTag", "SingleClick:"+feed.user_name);
				/*
				RemoteApiImpl remoteApi = new RemoteApiImpl();
				String userId = LocalStore.getLocaleStoreInstance().getUid();
				String emailString = remoteApi.getEmail(mContext, userId);
				*/
				/*
				recentUid = feed.uid;
				startRejectThread();
				*/
				recentUid = feed.uid;
				startAttentThread();
			}
		});
		
		if(feed.box_checked){
			attentItem.attentCheckBox.setChecked(true);
		}
		else{
			attentItem.attentCheckBox.setChecked(false);
		}
		attentItem.attentCheckBox.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				feed.box_checked = !feed.box_checked;
				if(feed.box_checked){
					Log.e("MyTag", "Checked"+feed.user_name);
				}
				else{
					Log.e("MyTag", "Dischecked"+feed.user_name);
				}
			}
		});
		
		return convertView;
	}

	
	public void setCheckBoxOnOff(int position){

		final Feed feed = mFeeds.get(position);
		feed.box_checked = !feed.box_checked;
		if(feed.box_checked){
			Log.e("MyTag", "Checked"+feed.user_name);
		}
		else{
			Log.e("MyTag", "Dischecked"+feed.user_name);
		}
	}

	private void startRejectThread()
	{
		Thread rejectThread = new Thread() 
		{
			public void run()
			{
				final String uid = LocalStore.getLocaleStoreInstance().getUid();
				
				String otherUid = recentUid;
				RemoteApiImpl remoteApi = new RemoteApiImpl();
				boolean ret = remoteApi.followSomeone(mContext, otherUid, true);
				
				if(ret)
				{
					finishedUid = otherUid;
					Message msg = new Message();
					msg.what = Constants.MSG_REJECT_SINGLE_FINISH;
					mHandler.sendMessage(msg);
				}
			}
		};
		rejectThread.start();
	}

	private void startAttentThread()
	{
		Thread attentThread = new Thread() 
		{
			public void run()
			{
				final String uid = LocalStore.getLocaleStoreInstance().getUid();
				
				String otherUid = recentUid;
				RemoteApiImpl remoteApi = new RemoteApiImpl();
				boolean ret = remoteApi.followSomeone(mContext, otherUid, true);
				
				if(ret)
				{
					finishedUid = otherUid;
					Message msg = new Message();
					msg.what = Constants.MSG_ACCEPT_SINGLE_FINISH;
					mHandler.sendMessage(msg);
				}
			}
		};
		attentThread.start();
	}

	class RequestAttentItem {
		ImageView mUserHeaderIcon;
		ImageView weiboIcon;
		TextView usernameText;
		TextView attentStatusText;
		Button attentButton;
		ImageView attentIcon;
		CheckBox attentCheckBox;
	}
}
